package com.demo.math.templateabstract;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.word.Word07Writer;
import com.demo.math.app.QuestionBankContext;
import com.demo.math.app.Transaction;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;

import java.awt.*;
import java.util.List;

public abstract class QuestionBankTemplate implements Transaction {


    private int page = 1;

   protected QuestionBankContext questionBankContext = new QuestionBankContext();


    public QuestionBankTemplate(int page) {
        this.page = page;
    }

    public abstract String title();

    public abstract List<String> body();

    @Override
    public void execute() {



        Word07Writer writer = new Word07Writer();
        try {
            String path = "d:/math/" + title() + "-" + DateUtil.date().toString("yyyyMMddHHmmss") + ".docx";
            for (int i = 0; i < page; i++) {
                // 添加段落（标题）
                writer.addText(ParagraphAlignment.CENTER, new Font("方正小标宋简体", Font.PLAIN, 22), title());
                // 添加段落（正文）
                writer.addText(ParagraphAlignment.CENTER, new Font("宋体", Font.PLAIN, 15), begin());
                List<String> body = body();
                for (String s : body) {
                    // 添加段落（正文）
                    writer.addText(new Font("宋体", Font.PLAIN, 14), s);
                }
                // 添加段落（正文）
                writer.addText(ParagraphAlignment.CENTER, new Font("宋体", Font.PLAIN, 15), line());
                writer.addText(ParagraphAlignment.LEFT, new Font("宋体", Font.PLAIN, 13), end());
                // 写出到文件
                writer.flush(FileUtil.file(path));
            }

        } finally {
            // 关闭
            writer.close();
        }
    }

    protected String zanwei(int len) {

        String temp = "";
        for (int i = 0; i < len; i++) {
            temp += " ";
        }
        return temp;
    }

    private String begin() {
        return "姓名：____________   用时：____________ ";
    }

    private String end() {
        return "评价：";
    }

    private String line() {
        return "______________________________________________________";
    }
}
